<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  公共api 
 *
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zscommgr extends Controller
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }




    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');

        $param=" and w.is_delete=0 ";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and (w.title like '%$keyword%' or w.title_en like '%$keyword%') ";
        }

        $sql2="select w.*,c.id as exhibition_id,c.title as exhibition_title";
        $sql2.=" from crm_zsqudao as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        //var_dump($sql2);die();
        $list=Db::query($sql2);
        $sql21=" select count(w.id) as count1 ";
        $sql21.=" from crm_zsqudao as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     */
    public function add(){
        $com =new CommonPlus();
        $data['exhibition_id'] = $this->request->param('exhibition_id');
        $data['title']=$this->request->param('title');   // 中文
        $url=$this->request->param('url');// lujin
        if(empty($data['exhibition_id'])){
           return json(['code'=>0,'msg'=>'展会有误','data'=>'']);
        }
        if(empty($data['title'])){
            return json(['code'=>0,'msg'=>'渠道名称有误','data'=>'']);
        }
        if(empty($url)){
            return json(['code'=>0,'msg'=>'渠道地址有误','data'=>'']);
        }
        $data['create_time']=time();
        $res=$com->insert_com_dan($data, '', 'zsqudao');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        $url=$url.'&channel_id='.$res;
        $r = db('zsqudao')->where(['id'=>$res])->update(['url'=>$url]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('zsqudao')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('zsqudao')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        $data['exhibition_id'] = $this->request->param('exhibition_id');
        $data['title']=$this->request->param('title');   // 中文
        if(empty($data['exhibition_id'])){
            return json(['code'=>0,'msg'=>'展会有误','data'=>'']);
        }
        if(empty($data['title'])){
            return json(['code'=>0,'msg'=>'渠道名称有误','data'=>'']);
        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '渠道信息有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="zsqudao";
        $r=$com->edit_com_fxlist($data, $where, $tbname);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }
   
    /**
     *  展商 web 登录， 先查展会exbibition 在查所在表展商
     * 
 
     *  王五 123456
     */
    public function login_zs()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        $exhibition_id=$this->request->param('exhibition_id');
        $account=$this->request->param('account');
        $pwd=$this->request->param('pwd');

        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少参数exhibition_id','data'=>'']);
        }

        // ($data, $where, $tbname)
        // 先查展会  -展商
        $where=array('id'=>$exhibition_id);
        $zhinfo=$com->sel_com_one('ff', $where, 'exhibition');

        if (!$zhinfo) {
            return json(['code' => 0, 'msg' => '未查询到相关展会信息']);
        }

        $pwd=md5(md5($pwd));// pin
        $where1=array('account'=>$account,'pwd'=>$pwd);
        $zshanginfo=$com->sel_com_one('ff', $where1, $zhinfo['create_tablename']);// 获取展商
        // company_id1
        if (!$zshanginfo) {
            return json(['code' => 0, 'msg' => '未查询到相关展商信息']);
        }

        //var_dump($zsinfo);die;

         
        //$where['id'] = $id;
        //$tbname="zsqudao";
       // $r=$com->edit_com_fxlist($data, $where, $tbname);
         
        return json(['code' => 1, 'msg' => '操作成功','data'=>['zshanginfo'=>$zshanginfo]]);
    }
    /**
     * 1： 先查询所有参展历史。 带出展位 
     *   
     *     // 获取参展历史 st
     *   所有展会，展商信息
     *  9585 
     *  both_id 展会id
     */

    public function czLogList_s(){
        $company_id = input('company_id1');//基础库id
        $result=db('exhibition')->where(['is_delete'=>0])->select();
      
        $fields='id,company_name';
        $histroyList=array();
        foreach($result as $key =>&$v){
            $tabName=$v['create_tablename'];
            $company_public_company = 
             db($tabName)
           // ->where(['company_id1'=>$id,'status'=>6])
            ->where('company_id1='.$company_id.' and status>=4')
            ->field($fields)
            ->find();
         
            if(!empty($company_public_company)){            
                $company_public_company['id_zh']=$v['id'];
                $company_public_company['name_zh']=$v['title'];
                $company_public_company['tablename_zs']=$v['create_tablename'];
                $histroyList[]=$company_public_company;
            }

        }
        unset($v);
        // var_dump($histroyList);die;
         return json(['code'=>1,'msg'=>'success','data'=>$histroyList]); 
    }

     /**
     * 1：根据参展历史查询 展商信息 
     *   所有展会，展商信息
     *  9585 
     *  both_id 展位id 
     *  zh_20200528fmr
     */

    public function czinfo(){

        $company_id = input('company_id1'); // 展商id
        $tablename_zs = input('tablename_zs'); 

        if (!$tablename_zs ||  !$company_id) {
            return json(['code' => 0, 'msg' => '缺少参数']);
        } 
       $where=" zs.company_id1='".$company_id."'";
        $list = Db::name($tablename_zs)
             ->alias('zs')
             ->field('zs.*,b.area,b.price_type,b.number') 
             ->join('booth b','zs.both_id=b.id','left')
             ->where($where) 
             ->find();  
        return json(['code' => 1, 'msg' => '操作成功','data'=>['zshanginfo'=>$list]]);
    }



}
